'use strict'

define(['jquery', 'bootstrap', 'select2', 'validate', 'validate.en', 'koala-search', 
        'modalSearch', 'datetimepicker', 'datetimepickerCN', 'shiftform'], function ($, validate) {
  return {
  
    init: function (data) {
      
      this.data = data;
      this.existCode = false;
      this.existCitizen = false;
      this.existMobile = false;
      this.exist();

      this.initEditBasic();
      this.initEditProfile();
      
    },

    initEditBasic: function() {
      
      var that = this;
      
      $(document).on('shown.bs.modal', '#edit-basic', function(e) {
        
        // 1. check input validate
        $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
        
        // 2. check unique item,  submit
        var $form = $(e.currentTarget).find("form")                          //bind shiftform

        $form.shiftform({
          beforeSubmit: function (el) {
            $form.trigger("validate.ajax.submit");
            var exist = false;
            if (that.existCode || that.existCitizen || that.existMobile) {
              return false;
            }
            return $.bt_validate.result[$.bt_validate.form_id];
          }
        });
        
      });
      
    },
    
    initEditProfile: function() {
      $(document).on('shown.bs.modal', '#edit-profile', function(e) {
        var $form = $(e.currentTarget).find("form")                          //bind shiftform
        $form.shiftform();
      });
    },
    
    exist: function() {
      
      var that = this;
      
      // check code whether exist
      $(document).on('mouseleave blur', 'input[name="code"]', function(e) {
        
        var $btnSubmit = $('button.btn-submit');
        
        var code = $('input[name="code"]').val().trim();
        if (!code) return;
        if (that.data.code == code) {
          that.existCode = false;
          $btnSubmit.prop('disabled', false);
          return;
        }
          
        $.ajax({
          'url': window.__CTX__.root + "/drivers/exist-code",
          'data': {
            'code': code
          }
        }).done(function(res){
          
          res = typeof res === 'String' ? JSON.parse(res) : res;
          if (res.data.existed) {
            $("input[name='code']").show_err_tooltip(res.data.message);
            $btnSubmit.prop('disabled', true);
            that.existCode = true;
            return false;
          }
          
          that.existCode = false;
          $btnSubmit.prop('disabled', false);
          
        }).fail(function(){
          $("input[name='code']").show_err_tooltip('校验工号唯一性失败！');
          $btnSubmit.prop('disabled', true);
        });

      });
      
      // check citizenCode whether exist
      $(document).on('mouseleave blur', 'input[name="citizenCode"]', function(e) {

        var citizenCode = $('input[name="citizenCode"]').val().trim();

        citizenCode = citizenCode.toUpperCase();

        $('input[name="citizenCode"]').val(citizenCode);

        var $btnSubmit = $('button.btn-submit');
        
        if (!citizenCode) return;
        if (that.data.citizenCode == citizenCode) {
          that.existCitizen = false;
          $btnSubmit.prop('disabled', false);
          return;
        }
          
        $.ajax({
          'url': window.__CTX__.root + "/drivers/exist-citizen",
          'data': {
            'citizenCode': citizenCode
          }
        }).done(function(res){
          
          res = typeof res === 'String' ? JSON.parse(res) : res;
          if (res.data.existed) {
            $("input[name='citizenCode']").show_err_tooltip(res.data.message);
            that.existCitizen = true;
            $btnSubmit.prop('disabled', true);
            return false;
          }
          
          that.existCitizen = false;
          $btnSubmit.prop('disabled', false);
          
        }).fail(function(){
          $("input[name='citizenCode']").show_err_tooltip('校验身份证号唯一性失败！');
          $btnSubmit.prop('disabled', true);
        });
        
      });  
        
      // check citizenCode whether exist
      $(document).on('mouseleave blur', 'input[name="mp"]', function(e) {

        var $btnSubmit = $('button.btn-submit');
        
        var mobile = $('input[name="mp"]').val().trim();
        if (!mobile) return;
        if (that.data.mobile == mobile) {
          that.existMobile = false;
          $btnSubmit.prop('disabled', false);
          return;
        }
          
        $.ajax({
          'url': window.__CTX__.root + "/drivers/exist-mobile",
          'data': {
            'mobile': mobile
          }
        }).done(function(res){
          
          res = typeof res === 'String' ? JSON.parse(res) : res;
          if (res.data.existed) {
            $("input[name='mp']").show_err_tooltip(res.data.message);
            that.existMobile = true;
            $btnSubmit.prop('disabled', true);
            return false;
          }
          
          that.existMobile = false;
          $btnSubmit.prop('disabled', false);
          
        }).fail(function(){
          $("input[name='mp']").show_err_tooltip('校验手机号唯一性失败！');
          $btnSubmit.prop('disabled', true);
        });
      
      });
    }
  }
})